'''
Created on Apr 5, 2012

@author: loic
'''
from PyQt4 import QtCore
from PyQt4.QtGui import *
from mp3rename import *
from mp3renameui import *
from glob import glob
import os.path

TRACE=1

if __name__ == '__main__':
    import sys
    app = QtGui.QApplication(sys.argv)
    MainWindow = QtGui.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)

    tag = mp3()
#    eyeD3.frames.FORCE_ENCODING = eyeD3.frames.UTF_8_ENCODING

    filelist = glob(sys.argv[1]+'/*.mp3')
    filelist.sort()
    
    taglist = {}
    files = {}
    tagnb = 1
    filename_max_length = 0
    for f in filelist:
#        print f
        tag.link(f)
        filename_max_length = max(filename_max_length, len(os.path.basename(f)))
            
        files[f] = tag.get_frames_as_text()
        for frame in files[f]:
            if not frame in taglist:
                taglist[frame] = (tagnb, len(files[f][frame]))
                tagnb += 1
            else:
                (n, l) = taglist[frame]
                taglist[frame] = (n, max(l, len(files[f][frame])))
#                print "%s : %s" % (frame, files[f][frame])
#        if 'TRDA' in frames_as_text:
#            sp = frames_as_text['TRDA'].split('.')
#            frames_as_text['TRDA'] = "%s%s%s" % (sp[2], sp[1], sp[0])
#        print "%(TRDA)s_%(_TALB)s" % frames_as_text
        
#    colNum = ui.tableWidget.columnCount()
    print ui.tableWidget.columnCount()
    ui.tableWidget.insertColumn(0)
    ui.tableWidget.setColumnWidth(0, filename_max_length)
    i = 1
    for t in taglist:
        (n, l) = taglist[t]
        ui.tableWidget.insertColumn(i)
#        ui.tableWidget.setColumnWidth(i, l)
        i += 1
    
    headers = ['Nom']
    headers.extend(taglist.keys())
    ui.tableWidget.setHorizontalHeaderLabels(headers)
    MainWindow.show()
    for f in filelist:
        rowNum = ui.tableWidget.rowCount()
        ui.tableWidget.insertRow(rowNum)
        ui.tableWidget.setItem(rowNum, 0, QTableWidgetItem(unicode(os.path.basename(f))))
        for frame in files[f]:
            (n, l) = taglist[frame]
            ui.tableWidget.setItem(rowNum, n, QTableWidgetItem(files[f][frame]))
            
    sys.exit(app.exec_())
